package com.oo.array_$.case_2;

import java.util.Scanner;

/**
 * ChangeDemo--1、验证数据是否为正整数，如果不是，输出“数据无效”，并结束程序
 2、十进制转换为二进制的规则：用2辗转相除至结果为1，将余数和最后的1从下向上倒序写出就是结果
 如将十进制数 7 转换为二进制
 步骤为：
 7/2=3 余1
 3/2=1 余1
 则二进制为：111
 3、使用循环方式，实现数制转换

 *
 * @author: Jovi
 * @createTime: 2017-05-11 16:06
 **/
public class ChangeDemo {

    public static void main(String[] args) {
        /* Define an array data after conversion */
        int[] array = new int[32];
        /* Defined variable to hold the user receives data, define the counter variable */
        int num,j = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a positive integer");
        num = sc.nextInt();
        if (num < 1) {
            System.out.println("The data is invalid");
            return;
        }
        System.out.print("Decimal number：" + num + " corresponding to a binary number：");
        while (num != 0) {
            array[j] = num % 2;//Save the remainder
            j++;
            num /= 2;//Data is divided by 2 after operation
        }

        /* 倒序输出数组的成员 */
        for (int i = j-1; i >=0; i--) {
            System.out.print(array[i]);
        }
    }
}
